/*
 *  Copyright (C) 2010 Takaji
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * ConfigurationView.java
 *
 * Created on Jan 5, 2010, 2:09:33 PM
 */
package dakside.hacc.modules.system.config;

import dakside.hacc.core.config.ConfigEntry;
import dakside.hacc.core.config.Configuration;
import java.awt.Component;

import java.util.logging.Logger;
import org.dakside.duck.plugins.AppConstants;
import org.dakside.duck.plugins.Command;
import org.dakside.duck.plugins.CommandInvoker;
import org.dakside.duck.plugins.DefaultCommandInvoker;
import org.dakside.duck.plugins.ToolbarContainer;

/**
 *
 * @author Takaji
 */
public class ConfigurationView extends javax.swing.JPanel implements ToolbarContainer {

    private static final Logger logger = Logger.getLogger(ConfigurationView.class.getName());

    /** Creates new form ConfigurationView */
    public ConfigurationView() {
        initComponents();
        refresh();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        customToolbar = new javax.swing.JToolBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        customToolbar.setFloatable(false);
        customToolbar.setRollover(true);
        customToolbar.setName("customToolbar"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new ConfigurationTableModel());
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar customToolbar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    /**
     * save all configurations
     */
    private void saveAll() {
        ConfigurationTableModel model = (ConfigurationTableModel) jTable1.getModel();
        ConfigEntry[] allEntries = model.getObjectArray(new ConfigEntry[0]);
        for (ConfigEntry entry : allEntries) {
            Configuration.getInstance().saveConfig(entry);
        }
    }

    private void refresh() {
        ConfigurationTableModel model = (ConfigurationTableModel) jTable1.getModel();
        model.removeAll();
        //only list entries user can setup
        model.add(Configuration.getInstance().getOrCreateConfig(ConfigEntry.BASE_CURRENCY));
        model.add(Configuration.getInstance().getOrCreateConfig(ConfigEntry.LANGUAGE));
        //display config entries
        model.fireTableDataChanged();
    }
    
    // <editor-fold defaultstate="collapsed" desc="TOOLBAR CONTAINER SUPPORT">
    private Component[] toolbar;

    public Component[] getToolbarItems() {
        if (toolbar == null) {
            toolbar = customToolbar.getComponents();
        }
        return toolbar;
    }
    private DefaultCommandInvoker myInvoker = null;

    public final synchronized CommandInvoker getInvoker() {
        if (myInvoker == null) {
            myInvoker = new DefaultCommandInvoker();
        }
        myInvoker.add(AppConstants.Commands.SAVE, new Command() {

            public Object invoke(Object[] args) {
                saveAll();
                return true;
            }
        });
        myInvoker.add(AppConstants.Commands.REFRESH, new Command() {

            public Object invoke(Object[] args) {
                refresh();
                return true;
            }
        });
        return myInvoker;
    }
    //END OF TOOLBAR CONTAINER SUPPORT
    //</editor-fold>
}
